package com.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.api.entity.Result;
import com.api.entity.UserRoles;
import com.api.service.UserRolesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api("用户 和 角色")
/* @RequestMapping("/userRoles") */
@ApiIgnore
public class UserRolesComtroller {
	@Autowired
	private UserRolesService userRolesService;
	@ApiOperation("将指定用户添加到角色中")
	@PostMapping("/AddUserToRole")
	public Object addUsersRoles(@ApiParam("用户id")@RequestParam("uId") String uId,@ApiParam("角色id")@RequestParam("rId") String rId,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			UserRoles urs = new UserRoles();
			urs.setRoleid(rId);
			urs.setUserid(uId);
			int i = userRolesService.addUsersRoles(urs);
			if(i>0) {
				Result result = new Result("添加成功","",true);
				return result;
			}else {
				Result result = new Result("添加失败","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("从角色中移除指定的用户")
	@PostMapping("/RemoveUserFromRole")
	public Object deleteUsersRoles(@ApiParam("用户id")@RequestParam("uId") String uId,@ApiParam("角色id")@RequestParam("rId") String rId,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			UserRoles urs = new UserRoles();
			urs.setRoleid(rId);
			urs.setUserid(uId);
			int i = userRolesService.deleteUsersRoles(urs);
			if(i>0) {
				Result result = new Result("删除成功","",true);
				return result;
			}else {
				Result result = new Result("删除失败","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	
	
}
